package com.yhy.wxzn.wcs.device.rgv.bean;


import com.yhy.wxzn.wcs.device.rgv.core.lock.LockNodeManager;
import com.yhy.wxzn.wcs.device.rgv.enums.RgvStatus;
import com.yhy.wxzn.wcs.device.rgv.exception.AvoidInterceptedException;
import lombok.Getter;
import lombok.Setter;

import static com.yhy.wxzn.wcs.device.rgv.FourWayRgvTaskExecuteThread.RGV_THREAD_LOCAL;

@Getter
@Setter
public abstract class BaseRgv  implements Rgv{



    private volatile boolean intercepted;
    private volatile boolean avoidIntercepted;


    protected volatile RgvStatus rgvStatus = RgvStatus.READY;







    public void checkIntercept(String msg){
        if (intercepted){
            intercepted = false;
            String errorMsg = String.format("%s被手动中断,在%s", this.toString(),msg);
            throw new RuntimeException(errorMsg);
        }
    }

    public void checkInterceptAndReleaseLifterLock(String msg, Lifter lifter){
        if (intercepted){
            LockNodeManager.unlockLifter(lifter,RGV_THREAD_LOCAL.get());
            intercepted = false;
            String errorMsg = String.format("%s被手动中断,在%s", this.toString(),msg);
            throw new RuntimeException(errorMsg);
        }
    }


    public void checkAvoidIntercept(){
        if (avoidIntercepted){
            avoidIntercepted = false;
            throw new AvoidInterceptedException("当前RGV被其他RGV中断，准备执行避让任务");
        }
    }

    public abstract void freshRgvInfo(Coord dbLocation);
}
